from datetime import datetime

from dateutil.relativedelta import relativedelta
from django.db import models
from decimal import Decimal
from utils import utils


# Create your models here.
class ManagerList(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="id")
    user_name = models.CharField(max_length=10, verbose_name="用户名", )
    pwd = models.CharField(max_length=128, verbose_name="密码", )
    nick_name = models.CharField(max_length=20, verbose_name="昵称", null=True, blank=True, )
    level_ = (
        (1, "超级管理员"),
        (2, "店长"),
        (3, "店员"),
    )
    auth_level = models.SmallIntegerField(verbose_name="权限", choices=level_, default=2)

    class Meta:
        managed = True
        db_table = 'manager_list'

    def __str__(self) -> str:
        return f"{self.id}. {self.user_name}   {self.nick_name}"


class CustomerInfo(models.Model):
    
    pid = models.AutoField(primary_key=True, verbose_name="编号")
    name = models.CharField(max_length=20, verbose_name="姓名")
    sex_choice = (
        (1, "男"),
        (2, "女"),
    )
    sex = models.SmallIntegerField(verbose_name="性别", choices=sex_choice, default=1)
    age = models.CharField(max_length=5, verbose_name="年龄", blank=True, null=True)
    birthday = models.DateField(blank=True, null=True, verbose_name="出生年月")
    phone = models.CharField(max_length=20, blank=True, null=True, verbose_name="手机号")
    cs_choice = (
        (1, "自主进店"),
        (2, "门店宣传单"),
        (3, "老客户推荐"),
        (4, "微信推荐"),
        (5, "公众号推荐"),
        (6, "其他"),
    )
    c_source = models.SmallIntegerField(verbose_name="客户来源", choices=cs_choice, default=1)
    c_source_self_defined = models.CharField(max_length=100, verbose_name="客户来源其他", blank=True, null=True)
    
    symptom = models.CharField(max_length=255, blank=True, null=True, verbose_name="主要症状")
    ill_date_period = models.CharField(max_length=5, verbose_name="病史", blank=True, null=True)
    date_be_ill = models.DateField(blank=True, null=True, verbose_name="首次发病日期")
    
    tb_choice = (
        (1, "试用"),
        (2, "已购买"),

    )
    create_time = models.DateTimeField(verbose_name="首次到店时间", default=datetime.now().strftime("%Y-%m-%d %H:%M"))
    trail_or_bought = models.SmallIntegerField(choices=tb_choice, default=1, verbose_name="试用/已购买")
    trial_counts = models.IntegerField(blank=True, null=True, verbose_name="试用次数", default=1)
    last_test_date = models.DateTimeField(verbose_name="末次进店时间", blank=True, null=True)
    returning_visits_date = models.DateTimeField(verbose_name="末次回访时间", blank=True, null=True)

    def __str__(self) -> str:
        return f"{self.pid}. {self.name}   {self.age}岁"

    class Meta:
        managed = True
        db_table = 'customer_info'

    @property
    def days_f_last_test_date(self):
        return relativedelta(datetime.now(), self.last_test_date).days

    @property
    def days_f_rv_date(self):
        return relativedelta(datetime.now(), self.returning_visits_date).days

    @property
    def age_of_c(self):
        return relativedelta(datetime.now(), self.create_time).days


class ReturningVisits(models.Model):
    rid = models.AutoField(primary_key=True, verbose_name="编号")
    pid = models.ForeignKey(CustomerInfo, models.CASCADE, db_column='pid', verbose_name="客户id", )
    create_time = models.DateTimeField(verbose_name="回访时间", )
    feedback = models.TextField(max_length=1024, blank=True, null=True, verbose_name="客户反馈")
    summary = models.TextField(max_length=1024, blank=True, null=True, verbose_name="回访总结")

    def __str__(self) -> str:
        return f"{self.rid}.{self.pid.name} {self.create_time}"

    class Meta:
        managed = True
        db_table = 'returning_visits'


class ItemPriceList(models.Model):
    item_id = models.AutoField(primary_key=True, verbose_name="产品编号")
    item_name = models.CharField(max_length=20, blank=True, null=True, verbose_name="产品名称")
    item_image = models.ImageField(verbose_name='产品图片', null=True, blank=True, upload_to='img/')
    item_introduction = models.TextField(max_length=1024, blank=True, null=True, verbose_name="产品描述")
    kind_ = (
        (1, "单项"),
        (2, "套餐"),
    )
    item_kind = models.SmallIntegerField(choices=kind_, default=1, verbose_name="产品类别")

    item_discount = models.DecimalField(max_digits=3, decimal_places=2, verbose_name="折扣", blank=True, null=True,
                                        default=Decimal("1.00"))
    item_price = models.SmallIntegerField(blank=True, null=True, verbose_name="产品价格")




    class Meta:
        managed = True
        db_table = 'items_price_list'

    def __str__(self) -> str:
        return f"{self.item_name} {self.item_price}元"


class BuyingList(models.Model):
    buying_id = models.AutoField(primary_key=True)
    buying_date = models.DateTimeField(blank=True, null=True, verbose_name="购买日期", default=utils.today_date())
    pid = models.ForeignKey(CustomerInfo, models.CASCADE, db_column='pid', verbose_name="客户id")
    order_id = models.CharField(max_length=20, blank=True, null=True, verbose_name="订单号", )

    bill_amount_bf_dst = models.IntegerField(blank=True, null=True, verbose_name="原价")
    total_amount = models.IntegerField(blank=True, null=True, verbose_name="订单金额")
    operator = models.ForeignKey(ManagerList, models.CASCADE, db_column='id', verbose_name="经办人", )
    status = (
        (1, "已支付"),
        (0, "未支付"),
        (2, "已作废"),
    )
    bill_status = models.SmallIntegerField(blank=True, null=True, verbose_name="订单状态", choices=status, default=0)

    class Meta:
        managed = True
        db_table = 'buying_list'

    @property
    def last_bought(self):
        return relativedelta(datetime.now(), self.buying_date).days


class OrderList(models.Model):
    oid = models.AutoField(primary_key=True)
    item_id = models.ForeignKey(ItemPriceList, models.CASCADE, db_column='item_id', verbose_name="商品编号", )
    order_id = models.ForeignKey(BuyingList, models.CASCADE, db_column='order_id', verbose_name="订单号", )
    order_quantity = models.IntegerField(blank=True, null=True, verbose_name="购买数量")

    class Meta:
        managed = True
        db_table = 'order_list'


class TestReport(models.Model):
    tid = models.AutoField(primary_key=True, verbose_name="检查结果报告id")
    test_result = models.TextField(max_length=1028, blank=True, null=True, verbose_name="检查结果报告")
    reporter = models.ForeignKey(ManagerList, models.CASCADE, db_column='id', verbose_name="报告人", default=None)

    def __str__(self) -> str:
        return f"检查单编号：{self.tid}"


class TestList(models.Model):
    test_id = models.AutoField(primary_key=True, verbose_name="检查id")

    test_date = models.DateTimeField(blank=True, null=True, verbose_name="检查时间")
    tb_choice = (
        (1, "试用"),
        (2, "已购买"),

    )
    trail_or_bought = models.SmallIntegerField(choices=tb_choice, default=1, verbose_name="试用/已购买")
    pid = models.ForeignKey(CustomerInfo, models.CASCADE, db_column='pid', verbose_name="客户id")
    symptom = models.CharField(max_length=255, blank=True, null=True, verbose_name="本次症状")

    test_treatment = models.CharField(blank=True, null=True, max_length=50, verbose_name="检查治疗项目")
    test_result = models.TextField(max_length=255, blank=True, null=True, verbose_name="检查结果报告")
    tid = models.ForeignKey(TestReport, models.CASCADE, db_column='tid', verbose_name="检查结果id", blank=True, null=True)
    stf_choice = (
        (1, "满意，愿意继续治疗"),
        (2, "一般，复购意向不明确"),
        (3, "差，无复购意向"),
        (4, "满意，购买意向高"),
        (5, "一般，有购买意向"),
        (6, "差，无购买意向"),
        (7, "观望中，会继续试用"),

    )
    satisfaction = models.SmallIntegerField(blank=True, null=True, choices=stf_choice, verbose_name="客户满意度")
    feedback = models.TextField(max_length=1024, blank=True, null=True, verbose_name="客户反馈")

    class Meta:
        managed = True
        db_table = 'test_list'


class ImList(models.Model):
    im_id = models.AutoField(primary_key=True)
    im_path = models.CharField(max_length=50, blank=True, null=True, verbose_name="图像路径")
    test_id = models.ForeignKey(TestList, models.CASCADE, db_column='test_id', verbose_name="检查id", )

    class Meta:
        managed = True
        db_table = 'im_list'


class WorkingList(models.Model):
    working_id = models.AutoField(primary_key=True, verbose_name="业务编号")
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="记录时间")
    working_kind_choice = (
        (1, "记录新客户"),
        (2, "记录新进店记录"),
        (3, "生成鼻道镜报告"),
        (4, "更新鼻道镜报告"),
        (5, "达成新订单"),
        (6, "创建回访"),

    )
    working_kind = models.SmallIntegerField(blank=True, null=True, choices=working_kind_choice, verbose_name="业务分类")
    description = models.CharField(max_length=128, blank=True, null=True, verbose_name="业务描述")
    user_id = models.ForeignKey(ManagerList, models.CASCADE, db_column='id', verbose_name="用户id")
    pid = models.ForeignKey(CustomerInfo, models.CASCADE, db_column='pid', verbose_name="客户id", blank=True,
                            null=True)

    test_id = models.ForeignKey(TestList, models.CASCADE, db_column='test_id', verbose_name="检查id", blank=True,
                                null=True, related_name="working_test_id")
    tid = models.ForeignKey(TestReport, models.CASCADE, db_column='tid', verbose_name="检查报告", blank=True,
                            null=True, related_name="working_tid")
    buying_id = models.ForeignKey(BuyingList, models.CASCADE, db_column='buying_id', verbose_name="购买id", blank=True,
                                  null=True)
    rid = models.ForeignKey(ReturningVisits, models.CASCADE, db_column='rid', verbose_name="回访id", blank=True,
                            null=True)
